<template><div><h2 id="部署" tabindex="-1"><a class="header-anchor" href="#部署"><span>部署</span></a></h2>
<p>Yii 中文文档 /</p>
<h2 id="部署-配置-web-服务器" tabindex="-1"><a class="header-anchor" href="#部署-配置-web-服务器"><span>部署（配置 Web 服务器）</span></a></h2>
<blockquote>
<p>Tip: 如果你现在只是要试用 Yii 而不是将其部署到生产环境中， 本小节可以跳过。</p>
</blockquote>
<p>通过上述方法安装的应用程序在 Windows，Max OS X， Linux 中的 <a href="http://httpd.apache.org/" target="_blank" rel="noopener noreferrer">Apache HTTP 服务器</a> 或 <a href="http://nginx.org/" target="_blank" rel="noopener noreferrer">Nginx HTTP 服务器</a> 且 PHP 版本为 5.4 或更高都可以直接运行。 Yii 2.0 也兼容 Facebook 公司的 <a href="http://hhvm.com/" target="_blank" rel="noopener noreferrer">HHVM</a>， 由于 HHVM 和标准 PHP 在边界案例上有些地方略有不同，在使用 HHVM 时需稍作处理。</p>
<p>在生产环境的服务器上，你可能会想配置服务器让应用程序可以通过 URL <code v-pre>http://www.example.com/index.php</code> 访问而不是 <code v-pre>http://www.example.com/basic/web/index.php</code>。 这种配置需要将 Web 服务器的文档根目录(document root)指向 <code v-pre>basic/web</code> 目录。 可能你还会想隐藏掉 URL 中的 <code v-pre>index.php</code>，具体细节在 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/runtime-routing.md" target="_blank" rel="noopener noreferrer">URL 解析和生成</a> 一章中有介绍， 你将学到如何配置 Apache 或 Nginx 服务器实现这些目标。</p>
<blockquote>
<p>Info: 将 <code v-pre>basic/web</code> 设置为文档根目录(document root)，可以防止终端用户访问 <code v-pre>basic/web</code> 相邻目录中 的私有应用代码和敏感数据文件。 禁止对其他目录的访问是一个不错的安全改进。</p>
</blockquote>
<blockquote>
<p>Info: 如果你的应用程序将来要运行在共享虚拟主机环境中， 没有修改其 Web 服务器配置的权限，你依然可以通过调整应用的结构来提升安全性。 详情请参考 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/tutorial-shared-hosting.md" target="_blank" rel="noopener noreferrer">共享主机环境</a> 一章。</p>
</blockquote>
<blockquote>
<p>Info: 如果您在反向代理后面运行Yii应用程序， 则可能需要在请求组件中配置 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/runtime-requests.md#trusted-proxies" target="_blank" rel="noopener noreferrer">Trusted proxies and headers</a>。</p>
</blockquote>
<h2 id="apache-推荐配置" tabindex="-1"><a class="header-anchor" href="#apache-推荐配置"><span>Apache 推荐配置</span></a></h2>
<p>在 Apache 的 <code v-pre>httpd.conf</code> 文件或在一个<code v-pre>httpd-vhosts.conf</code>（虚拟主机配置）文件中使用如下配置。 注意，你应该将 <code v-pre>path/to/basic/web</code> 替换为实际的 <code v-pre>basic/web</code> 目录。</p>
<div class="language-apache line-numbers-mode" data-highlighter="prismjs" data-ext="apache" data-title="apache"><pre v-pre class="language-apache"><code><span class="line"># 设置文档根目录为 &quot;basic/web&quot;</span>
<span class="line">DocumentRoot &quot;path/to/basic/web&quot;</span>
<span class="line"></span>
<span class="line">&lt;Directory &quot;path/to/basic/web&quot;&gt;</span>
<span class="line">    # 开启 mod_rewrite 用于美化 URL 功能的支持（译注：对应 pretty URL 选项）</span>
<span class="line">    RewriteEngine on</span>
<span class="line">    # 如果请求的是真实存在的文件或目录，直接访问</span>
<span class="line">    RewriteCond %{REQUEST_FILENAME} !-f</span>
<span class="line">    RewriteCond %{REQUEST_FILENAME} !-d</span>
<span class="line">    # 如果请求的不是真实文件或目录，分发请求至 index.php</span>
<span class="line">    RewriteRule . index.php</span>
<span class="line"></span>
<span class="line">    # if $showScriptName is false in UrlManager, do not allow accessing URLs with script name</span>
<span class="line">    RewriteRule ^index.php/ - [L,R=404]</span>
<span class="line"></span>
<span class="line">    # ...其它设置...</span>
<span class="line">&lt;/Directory&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="nginx-推荐配置" tabindex="-1"><a class="header-anchor" href="#nginx-推荐配置"><span>Nginx 推荐配置</span></a></h2>
<p>为了使用 <a href="http://wiki.nginx.org/" target="_blank" rel="noopener noreferrer">Nginx</a>，你应该已经将 PHP 安装为 <a href="https://www.php.net/install.fpm" target="_blank" rel="noopener noreferrer">FPM SAPI</a> 了。 你可以使用如下 Nginx 配置，将 <code v-pre>path/to/basic/web</code> 替换为实际的 <code v-pre>basic/web</code> 目录， <code v-pre>mysite.local</code> 替换为实际的主机名以提供服务。</p>
<div class="language-nginx line-numbers-mode" data-highlighter="prismjs" data-ext="nginx" data-title="nginx"><pre v-pre class="language-nginx"><code><span class="line"><span class="token directive"><span class="token keyword">server</span></span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">charset</span> utf-8</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">client_max_body_size</span> <span class="token number">128M</span></span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">listen</span> <span class="token number">80</span></span><span class="token punctuation">;</span> <span class="token comment">## listen for ipv4</span></span>
<span class="line">    <span class="token comment">#listen [::]:80 default_server ipv6only=on; ## listen for ipv6</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">server_name</span> mysite.test</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">root</span>        /path/to/basic/web</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">index</span>       index.php</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">access_log</span>  /path/to/basic/log/access.log</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">error_log</span>   /path/to/basic/log/error.log</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> /</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment"># Redirect everything that isn't a real file to index.php</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">try_files</span> <span class="token variable">$uri</span> <span class="token variable">$uri</span>/ /index.php<span class="token variable">$is_args</span><span class="token variable">$args</span></span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment"># uncomment to avoid processing of calls to non-existing static files by Yii</span></span>
<span class="line">    <span class="token comment">#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {</span></span>
<span class="line">    <span class="token comment">#    try_files $uri =404;</span></span>
<span class="line">    <span class="token comment">#}</span></span>
<span class="line">    <span class="token comment">#error_page 404 /404.html;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment"># deny accessing php files for the /assets directory</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> ~ ^/assets/.*\.php$</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">deny</span> all</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> ~ \.php$</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">include</span> fastcgi_params</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">fastcgi_param</span> SCRIPT_FILENAME <span class="token variable">$document_root</span><span class="token variable">$fastcgi_script_name</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">fastcgi_pass</span> 127.0.0.1:9000</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token comment">#fastcgi_pass unix:/var/run/php5-fpm.sock;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">try_files</span> <span class="token variable">$uri</span> =404</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> ~* /\.</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">deny</span> all</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>使用该配置时，你还应该在 <code v-pre>php.ini</code> 文件中设置 <code v-pre>cgi.fix_pathinfo=0</code> ， 能避免掉很多不必要的 <code v-pre>stat()</code> 系统调用。</p>
<p>还要注意当运行一个 HTTPS 服务器时，需要添加 <code v-pre>fastcgi_param HTTPS on;</code> 一行， 这样 Yii 才能正确地判断连接是否安全。</p>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


